package com.cskaoyan.service.wx.zjh;

import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.pojo.MarketCollect;
import com.cskaoyan.module.pojo.MarketCollectExample;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.vo.wx.zjh.CollectListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/11/10:49
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    MarketCollectMapper collectMapper;


    BaseRespVo baseRespVo = new BaseRespVo();

    @Override
    public BaseRespVo addordelete(Integer id, Integer type, Integer valueId) {
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(Byte.valueOf(Integer.toString(type)));
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        //没有就插入，有就删除
        MarketCollect marketCollect = new MarketCollect();

        if (marketCollects.size()==0) {
            marketCollect.setUserId(id);
            marketCollect.setValueId(valueId);
            marketCollect.setType(Byte.valueOf(Integer.toString(type)));
            marketCollect.setAddTime(new Date());
            marketCollect.setUpdateTime(new Date());
            marketCollect.setDeleted(false);
            collectMapper.insert(marketCollect);
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
            baseRespVo.setData(null);

            return baseRespVo;
        } else {
            MarketCollect marketCollect1 = marketCollects.get(0);
            Integer id1 = marketCollect1.getId();
            collectMapper.deleteByPrimaryKey(id1);
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
            baseRespVo.setData(null);
            return baseRespVo;
        }
    }

    @Override
    public BaseRespVo listCollect(Integer id, Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);

        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(Byte.valueOf(Integer.toString(type)));
        criteria.andUserIdEqualTo(id);

        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);

        PageInfo<MarketCollect> infos = new PageInfo<>(marketCollects);

        CollectListVO collectListVO = new CollectListVO((int) infos.getTotal(), infos.getPages(), infos.getPageSize(), infos.getPageNum(), marketCollects);

        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(collectListVO);
        return baseRespVo;
    }
}
